/*
Anleitung zur Ausführung:
- Scala-Interpreter starten
- Code mit ':l <pfad>/aufg13.scala' laden
- Die relevanten Funktionen a und b werden automatisch am Ende des Ladevorgangs ausgeführt.
*/
import scala.math.Numeric.DoubleIsFractional

def abs = DoubleIsFractional.abs _

val delta = 1.0/8.0
var deviation = 1.0
var estimatedRTT = 4.0
var sampleRTT = 1.0
var steps = 0
var timeout = 0.0
 
def approxEstimatedRTT : Double = 
	estimatedRTT + ( delta * (sampleRTT - estimatedRTT))

def approxDeviation : Double =
	deviation + delta * (abs(sampleRTT - estimatedRTT) - deviation)

def approxTimeout : Double =
	estimatedRTT + 4.0 * deviation

def a = {
	do {
		estimatedRTT = approxEstimatedRTT
		deviation = approxDeviation
		timeout = approxTimeout
		steps += 1 
		/*println("steps: "+steps+
				"\testimatedRTT: "+estimatedRTT+
				"\tdeviation: "+deviation+
				"\ttimeout: "+timeout)*/
		} while(timeout > 4.0) 
	steps	
}

var n = 1

def b = {
	do {
		steps = 0
		estimatedRTT = 1.0
		do {
			steps += 1
			if(steps%n==0) sampleRTT = 4.0
			else sampleRTT = 1.0
			estimatedRTT = approxEstimatedRTT
			deviation = approxDeviation
			timeout = approxTimeout
		} while(steps <= n)
		n += 1
	} while(timeout > 4.0)
	n-1
}

//execute
println("\n\nLösungen für Aufgabe 13:")
println("a) #iteration = "+a)
println("b) max n      = "+b)
